import Vue from 'vue'
import VueRouter from 'vue-router'
import Welcome from '@/views/welcome/inedx'
import { getItemS } from '@/utils/sessionStorage'
import { TOKENKEY } from '@/constants'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'welcome',
    component: Welcome,
    children: [
      {
        path: '',
        component: () => import('@/views/welcome/huanying')
      },
      {
        path: 'users',
        name: 'users',
        component: () => import('@/views/users')
      },
      {
        path: 'roles',
        name: 'roles',
        component: () => import('@/views/roles')
      },
      {
        path: 'rights',
        name: 'rights',
        component: () => import('@/views/rights')
      },
      {
        path: 'goods',
        name: 'goods',
        component: () => import('@/views/commodity/goods')
      },
      {
        path: 'goods/add',
        name: 'addGoods',
        component: () => import('@/views/commodity/components/AddGoods')
      },
      {
        path: 'params',
        name: 'params',
        component: () => import('@/views/commodity/params')
      },
      {
        path: 'categories',
        name: 'categories',
        component: () => import('@/views/commodity/categories')
      },
      {
        path: 'orders',
        name: 'orders',
        component: () => import('@/views/orders')
      },
      {
        path: 'reports',
        name: 'reports',
        component: () => import('@/views/reports')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.name !== 'login' && !getItemS(TOKENKEY)) {
    next({ name: 'login' })
  } else {
    next()
  }
})
export default router
